package com.java.exer_3;

import java.util.Arrays;
import java.util.Comparator;

/**
 * @author shkstart
 * @create 2021-01-09 17:49
 */
public class CompareTest_2 {
    public static void main(String[] args) {

        CompareTest_1[] c1 = new CompareTest_1[5];
        c1[0] = new CompareTest_1("xiaoming", 20);
        c1[1] = new CompareTest_1("xiaohua", 21);
        c1[2] = new CompareTest_1("libai", 22);
        c1[3] = new CompareTest_1("dufu", 24);
        c1[4] = new CompareTest_1("lipeng", 28);

        Arrays.sort(c1, new Comparator() {

            @Override
            public int compare(Object o1, Object o2) {
                if (o1 instanceof CompareTest_1 && o2 instanceof CompareTest_1) {
                    CompareTest_1 c1 = (CompareTest_1) o1;
                    CompareTest_1 c2 = (CompareTest_1) o2;
                    if (c1.getAge() > c2.getAge()) {
                        return -1;

                    } else if (c1.getAge() < c2.getAge()) {
                        return 1;
                    } else if (c1.getAge() == c2.getAge()) {
                        return c1.getName().compareTo(c2.getName());
                    }

                }
                return 0;
            }
        });

        System.out.println(Arrays.toString(c1));

    }

}